(function() {
  var validate_acct_info;

  validate_acct_info = function(form) {
    var acct_name, acct_no, cred_no, cred_type, result;
    acct_name = form.form('get value', 'acct_name');
    cred_no = form.form('get value', 'acct_cred_no');
    cred_type = form.form('get value', 'acct_cred_type');
    if (parseInt(cred_type) === 1 && cred_no.trim().length > 0) {
      result = validate_idno(cred_no, '收款账户开户人');
      if (!result.succ) {
        alert(result.errmsg);
        return false;
      }
    }
    if (cred_no.trim().length === 0 && acct_name.trim().length === 0) {
      alert('账户开户人姓名和证件号码必须至少填写一个');
      return false;
    }
    acct_no = form.form('get value', 'acct_no');
    if (acct_no.length > 0) {
      if (!validate_cardno(acct_no)) {
        if (!confirm("收款账号可能存在错误，选择［取消］按钮返回检查，或［确定］按钮继续提交.")) {
          return false;
        }
      }
    }
    return true;
  };

  $(function() {
    var form, record;
    form = $('form.main');
    form.form({
      inline: true,
      fields: {
        t20_merno: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        t20_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz0_merno: {
          optional: false,
          rules: [
            {
              type: 'minLength[4]'
            }
          ]
        },
        tz0_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz0_macalgo: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz0_tmk: {
          optional: false,
          rules: [
            {
              type: 'minLength[32]'
            }, {
              type: 'maxLength[32]'
            }
          ]
        },
        t20_fee: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        t60_merno: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        t60_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz1_merno: {
          optional: false,
          rules: [
            {
              type: 'minLength[4]'
            }
          ]
        },
        tz1_termid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz1_macalgo: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        tz1_tmk: {
          optional: false,
          rules: [
            {
              type: 'minLength[32]'
            }, {
              type: 'maxLength[32]'
            }
          ]
        },
        t60_fee: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        p2_fund: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fee_take_type: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        fund_lack_proc: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        name: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        contact: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        address: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        email: {
          optional: true,
          rules: [
            {
              type: 'email'
            }
          ]
        },
        acct_no: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'integer[..]'
            }
          ]
        },
        acct_cred_type: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        acct_mobile: {
          optional: true,
          rules: [
            {
              type: 'integer[..]'
            }
          ]
        },
        pay_chnl: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        pay_merid: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        }
      }
    });
    freeze_form(form);
    record = form.form('get value', 'record');
    if (record && record.trim().length > 0) {
      form.form('set value', 'record', '');
      record = JSON.parse(record);
      form.form('set values', record);
    }
    form.find('button#reject').click(function() {
      var action, reason;
      if ($(this).hasClass('has_clicked')) {
        reason = form.form('get value', 'reject_reason');
        if (!reason || reason.trim().length === 0) {
          alert('请输入拒绝的原因，以便录入人员可以有针对性的进行修改');
          return;
        }
        $(this).removeClass('has_clicked').text('拒 绝');
        action = form.attr('action');
        form.attr('action', action + '/reject');
        form.submit();
        return form.attr('action', action);
      } else {
        form.find('button#pass').text('通 过').removeClass('has_clicked');
        form.find('.fields#effect_time').hide();
        form.find('.fields#reject_reason').show();
        return $(this).addClass('has_clicked').text('确认拒绝');
      }
    });
    return form.find('button#pass').click(function() {
      var action, effect_time;
      if (!form.form('is valid')) {
        return;
      }
      if (!validate_acct_info(form)) {
        return;
      }
      if ($(this).hasClass('has_clicked')) {
        effect_time = form.form('get value', 'effect_time');
        if (!effect_time || effect_time.trim().length === 0) {
          alert('请选择信息生效的时间');
          return;
        }
        $(this).removeClass('has_clicked').text('通 过');
        action = form.attr('action');
        form.attr('action', action + '/pass');
        form.submit();
        return form.attr('action', action);
      } else {
        form.find('button#reject').text('拒 绝').removeClass('has_clicked');
        form.find('.fields#reject_reason').hide();
        form.find('.fields#effect_time').show();
        return $(this).addClass('has_clicked').text('确认通过');
      }
    });
  });

}).call(this);
